<?php
/*
 * @Author: an
 * @Date: 2021/12/14
 * @Description: 意见反馈
 */


namespace App\Http\Controllers\WebApi;

use App\Models\ImFeedback;
use App\Models\ImFeedbackProcess;
use App\Models\ImReport;
use App\Models\ImReportCate;
use App\Models\ImReportProcess;
use Illuminate\Http\Request;

class FeedbackController
{
    /**
     * 意见反馈列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-14 14:23
     */
    public function feedbackList(Request $request)
    {
        $status = $request->get('status');
        $timeStart = $request->get('timeStart');
        $timeEnd = $request->get('timeEnd');
        $searchWord = $request->get('searchWord');

        list($offset, $limit, $page, $pagesize) = ImFeedback::pageProcess();

        $query = ImFeedback::with(['user']);

        //状态搜索：1待处理 2处理中 3已处理
        if ($status > 0) {
            $query->where('status', $status);
        }
        //时间搜索
        if (!empty($timeStart) && !empty($timeEnd)) {
            $query->whereBetween('created_at', [$timeStart, $timeEnd]);
        }
        //关键词搜索
        if (!empty($searchWord)) {
            $query->where(function ($sql) use ($searchWord) {
                $sql->where('content', 'like', '%' . $searchWord . '%');
                $sql->orWhere('user_id', 'like', '%' . $searchWord . '%');
            });
        }

        $total = $query->count();

        $data = $query->offset($offset)->limit($limit)
            ->orderBy('id', 'desc')->get();

        return okBack($data, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }

    /**
     * 反馈进度编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 14:29
     */
    public function process(Request $request)
    {
        $operator = $request->userInfo['id'];
        $feedback_id = $request->post('feedback_id');
        $status = $request->post('status');
        $remark = $request->post('remark');
        if ($feedback_id <= 0 || $status <= 0 || empty($remark)) {
            return errBack();
        }
        $feedback = ImFeedback::find($feedback_id);
        if (!$feedback) {
            return errBack('反馈信息有误');
        }
        $feedback->status = $status;
        $feedback->save();

        $feedbackProcess = new ImFeedbackProcess();
        $feedbackProcess->feedback_id = $feedback_id;
        $feedbackProcess->status = $status;
        $feedbackProcess->operator = $operator;
        $feedbackProcess->remark = $remark;
        $feedbackProcess->save();

        return okBack();
    }

    /**
     * 举报进度处理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-22 10:02
     */
    public function reportProcess(Request $request)
    {
        $operator = $request->userInfo['id'];
        $report_id = $request->post('report_id');
        $status = $request->post('status');
        $remark = $request->post('remark');
        if ($report_id <= 0 || $status <= 0 || empty($remark)) {
            return errBack();
        }
        $report = ImReport::find($report_id);
        if (!$report) {
            return errBack('举报信息有误');
        }
        $report->status = $status;
        $report->save();

        $reportProcess = new ImReportProcess();
        $reportProcess->report_id = $report_id;
        $reportProcess->status = $status;
        $reportProcess->operator = $operator;
        $reportProcess->remark = $remark;
        $reportProcess->save();

        return okBack();
    }

    /**
     * 举报明细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-22 10:09
     */
    public function reportDetails(Request $request)
    {
        $report_id = $request->get('report_id');
        if ($report_id <= 0) {
            return errBack();
        }
        $query = ImReport::with(['process.operator']);
        $data = $query->find($report_id);
        return okBack($data);
    }

    /**
     * 反馈明细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 15:08
     */
    public function feedbackDetails(Request $request)
    {
        $feedback_id = $request->get('feedback_id');
        if ($feedback_id <= 0) {
            return errBack();
        }
        $query = ImFeedback::with(['process.operator']);
        $data = $query->find($feedback_id);
        return okBack($data);
    }

    /**
     * 投诉举报列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-14 16:24
     */
    public function reportList(Request $request)
    {
        $status = $request->get('status');//状态：1待处理 2处理中 3已处理
        $cate_id = $request->get('cate_id');//举报类型ID
        $timeStart = $request->get('timeStart');//开始时间
        $timeEnd = $request->get('timeEnd');//结束时间
        $searchWord = $request->get('searchWord');//关键词搜索

        list($offset, $limit, $page, $pagesize) = ImReport::pageProcess();

        $query = ImReport::with(['user', 'otherUser', 'cate']);

        //处理状态搜索
        if ($status > 0) {
            $query->where('status', $status);
        }
        //举报类型搜索
        if ($cate_id > 0) {
            $query->where('cate_id', $cate_id);
        }
        //举报时间搜索
        if (!empty($timeStart) && !empty($timeEnd)) {
            $query->whereBetween('created_at', [$timeStart, $timeEnd]);
        }
        //关键搜索，举报者昵称
        if (!empty($searchWord)) {
            $query->whereHas('user', function ($sql) use ($searchWord) {
                $sql->where('nickname', 'like', '%' . $searchWord . '%');
            });
        }

        $total = $query->count();

        $data = $query->offset($offset)->limit($limit)
            ->orderBy('id', 'desc')->get();

        return okBack($data, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }

    /**
     * 举报原因列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 18:32
     */
    public function reportCateList(Request $request)
    {
        $data = ImReportCate::orderBy('sort', 'asc')->get();
        return okBack($data);
    }

    /**
     * 添加举报原因
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-20 16:10
     */
    public function reportCateAdd(Request $request)
    {
        $cate_name = $request->post('cate_name');
        $sort = $request->post('sort');
        $status = $request->post('status');
        if (empty($cate_name) || !is_numeric($sort) || !is_numeric($status)) {
            return errBack();
        }
        $cate = new ImReportCate();
        $count = $cate->where('cate_name', $cate_name)->count();
        if ($count) {
            return errBack('名称重复');
        }
        $cate->cate_name = $cate_name;
        $cate->sort = $sort;
        $cate->status = $status;
        $cate->save();
        return okBack();
    }

    /**
     * 编辑举报原因
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-20 16:11
     */
    public function reportCateEdit(Request $request)
    {
        $cate_id = $request->post('cate_id');
        $cate_name = $request->post('cate_name');
        $sort = $request->post('sort');
        $status = $request->post('status');
        if ($cate_id <= 0 || empty($cate_name) || !is_numeric($sort) || !is_numeric($status)) {
            return errBack();
        }

        $cate = ImReportCate::where('id', $cate_id)->first();
        if (!$cate) {
            return errBack('数据不存在');
        }

        $count = ImReportCate::where('cate_name', $cate_name)
            ->where('id', '<>', $cate_id)->count();
        if ($count) {
            return errBack('名称重复');
        }

        $cate->cate_name = $cate_name;
        $cate->sort = $sort;
        $cate->status = $status;
        $cate->save();
        return okBack();
    }

    /**
     * 删除举报原因
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-20 16:15
     */
    public function reportCateDel(Request $request)
    {
        $cate_id = $request->post('cate_id');
        if ($cate_id <= 0) {
            return errBack();
        }
        $cate = ImReportCate::where('id', $cate_id)->first();
        if (!$cate) {
            return errBack('数据不存在');
        }
        $cate->delete();
        return okBack();
    }

    /**
     * 查看意见反馈处理进度
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-22 15:18
     */
    public function feedbackProcessDetails(Request $request)
    {
        $feedback_id = $request->get('feedback_id');
        if ($feedback_id <= 0) {
            return errBack();
        }
        $query = ImFeedbackProcess::with(['operator']);
        $data = $query->where('feedback_id', $feedback_id)
            ->orderBy('id', 'desc')
            ->get();
        return okBack($data);
    }

    /**
     * 查看举报处理进度
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-22 15:18
     */
    public function reportProcessDetails(Request $request)
    {
        $report_id = $request->get('report_id');
        if ($report_id <= 0) {
            return errBack();
        }
        $query = ImReportProcess::with(['operator']);
        $data = $query->where('report_id', $report_id)
            ->orderBy('id', 'desc')
            ->get();
        return okBack($data);
    }
}
